package com.senlan.water.common.result;

import lombok.Data;

import java.io.Serializable;

@Data
public class BaseResult<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 成功标志
     */
    private Boolean success;

    /**
     * 消息
     */
    private String message;

    /**
     * 返回代码
     */
    private Integer code;

    /**
     * 时间戳
     */
    private long timestamp = System.currentTimeMillis();

    /**
     * 结果对象
     */
    private T result;

    /**
     * 构建
     * @return
     */
    private static <T> BaseResult<T> build(){
        BaseResult<T> baseResult = new BaseResult<>();
        baseResult.setSuccess(true);
        baseResult.setMessage("success");
        baseResult.setCode(ResultCode.SUCCESS.getCode());
        return baseResult;
    }

    public static <T> BaseResult<T> data(T t) {
        BaseResult<T> baseResult = build();
        baseResult.setResult(t);
        return baseResult;
    }

    public static <T> BaseResult<T> error(ResultCode resultCode) {
        BaseResult<T> baseResult = build();
        baseResult.setSuccess(false);
        baseResult.setMessage(resultCode.getMessage());
        baseResult.setCode(resultCode.getCode());
        return baseResult;
    }

    public static <T> BaseResult<T> error(int code,String message) {
        BaseResult<T> baseResult = build();
        baseResult.setSuccess(false);
        baseResult.setMessage(message);
        baseResult.setCode(code);
        return baseResult;
    }

    public static <T> BaseResult<T> success() {
        return build();
    }
}
